package com.zh.webcommon.common.exception;

import com.zh.webcommon.common.viewdata.ApiResult;
import org.slf4j.Logger;

import java.util.Arrays;

/**
 * 用于远程调用时的fallback时封装错误结果
 *
 * @author Zhou
 */
public interface FallbackResult {

    Throwable throwable();

    Logger log();

    default <T> ApiResult<T> failResult() {
        var className = Arrays.stream(this.getClass().getInterfaces()).filter(e -> e != FallbackResult.class).findAny().orElse(this.getClass()).getSimpleName();
        var methodName = Thread.currentThread().getStackTrace()[2].getMethodName();

        log().error("[远程调用{}.{}服务异常] exception: {};\t msg: {}", className, methodName, throwable().getClass(), throwable().getMessage());
        return ApiResult.fail("远程调用服务异常");
    }

}